<template>
  <PageWrapper title="修改当前用户密码" content="修改成功后会自动退出当前登录！">
    <div class="py-8 bg-white flex flex-col justify-center items-center">
      <BasicForm @register="register" />
      <div class="flex justify-center">
        <a-button @click="resetFields"> 重置 </a-button>
        <a-button class="!ml-4" type="primary" @click="handleSubmit"> 确认 </a-button>
      </div>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { router } from '@/router';
  import { PageWrapper } from '@/components/Page';
  import { BasicForm, useForm } from '@/components/Form';

  import { ChangePasswordParams } from '@/api/system/model/systemModel';
  import { ChangePassword } from '@/api/system/user';
  import { formSchema } from './pwd.data';
  import { useUserStore } from '@/store/modules/user';

  import { PageEnum } from '@/enums/pageEnum';
  import { useMessage } from '@/hooks/web/useMessage';

  const userStore = useUserStore();
  const { createSuccessModal, createErrorModal } = useMessage();

  defineOptions({ name: 'ChangePassword' });

  const [register, { validate, resetFields }] = useForm({
    size: 'large',
    baseColProps: { span: 24 },
    labelWidth: 100,
    showActionButtonGroup: false,
    schemas: formSchema,
  });

  async function handleSubmit() {
    try {
      const values = await validate();
      // TODO custom api
      console.log(values);
      // 参数组装
      const { passwordOld, passwordNew, confirmPassword } = values;
      let params: ChangePasswordParams = {
        username: userStore.getUserInfo.username,
        passwordOld: passwordOld,
        passwordNew: passwordNew,
        passwordConfirm: confirmPassword,
      };
      const { code, message } = await ChangePassword(params);
      if (code === 0) {
        createSuccessModal({
          content: '密码修改成功',
          onOk: () => {
            console.log(passwordOld, passwordNew);
            userStore.logout(true);
          },
        });
      } else {
        createErrorModal({ content: message });
      }
    } catch (error) {
      console.error(error);
    }
  }
</script>
